
/*******************************************************************************
 * channel.hpp - channel for trasio
 *
 * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 ******************************************************************************/

#ifndef __BOQUE_TRASIO_CHANNEL_HPP_2010__
#define __BOQUE_TRASIO_CHANNEL_HPP_2010__

#include <sys/types.h>

#include <stdexcept>
#include <string>
#include <map>

#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>

#include "boque/buffesq.hpp"

#include "boque/trasio/broker.hpp"


namespace boque {
namespace trasio {


class Channel
{

public	:
	enum status_type {
		UNKNOWN = 0,
		ALIVE = 1,
		BUFFERED = 2,
	};

public:
    Channel( const std::string& address, const std::string& buf_dir );
    ~Channel();

public	:
    
	int		send( const std::string& data );
	int		send( const char* data, size_t size );

	int		relay( void );

public	:
	// the following two method will call by watcher thread
	void	enableRelay( void) 	{ _relay_enable = true; }
	void	disableRelay( void ) { _relay_enable = false; }

	// the relayer thread will watch relay status
	bool	isRelaying( void ) { return _relaying; }
	const bool	isRelaying( void ) const { return _relaying; }
	
	
	bool    inBufferedMode( void ) { return BUFFERED == _status; }
	const bool    inBufferedMode( void ) const { return BUFFERED == _status; }
	bool    inAliveMode( void ) { return ALIVE == _status; }
	const bool    inAliveMode( void ) const { return ALIVE == _status; }
	
	void    setWaiter( boost::thread* p ) { _waiter_p = p; }
	boost::thread*    getWaiter( void )   { return _waiter_p; }
	
private :
    Channel( const Channel& );
    Channel&    operator=( const Channel& );

private	:
	int		do_relay( void );

	void	parseAddress( const std::string& address, std::string& protocol, std::string& host, std::string& port );


private :
	// address format : tcp://127.0.0.1:1234
	std::string		_address;

	std::string		_buf_dir;

	BuffesQ			_buffesq;
	
	Broker*			_broker_p;
	
	boost::mutex	_mutex;

	status_type		_status;

	boost::thread*	_waiter_p;

	bool			_broker_connected;

	boost::mutex	_mutex_relay;

	bool			_relay_enable;
	
	bool            _relaying;
};	// class Channel


}	// namespace trasio
}	// namespace boque


#endif /* __BOQUE_TRASIO_CHANNEL_HPP_2010__*/

